package cn.invest.merry.module.invest.service.zcfzbase;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.invest.merry.module.invest.controller.admin.zcfzbase.vo.*;
import cn.invest.merry.module.invest.dal.dataobject.zcfzbase.ZcfzBaseDO;
import cn.invest.merry.framework.common.pojo.PageResult;
import cn.invest.merry.framework.common.pojo.PageParam;
import cn.invest.merry.framework.common.util.object.BeanUtils;

import cn.invest.merry.module.invest.dal.mysql.zcfzbase.ZcfzBaseMapper;

import static cn.invest.merry.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.invest.merry.module.invest.enums.ErrorCodeConstants.*;

/**
 * 个股报告期资产负债 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ZcfzBaseServiceImpl implements ZcfzBaseService {

    @Resource
    private ZcfzBaseMapper zcfzBaseMapper;

    @Override
    public Long createZcfzBase(ZcfzBaseSaveReqVO createReqVO) {
        // 插入
        ZcfzBaseDO zcfzBase = BeanUtils.toBean(createReqVO, ZcfzBaseDO.class);
        zcfzBaseMapper.insert(zcfzBase);
        // 返回
        return zcfzBase.getId();
    }

    @Override
    public void updateZcfzBase(ZcfzBaseSaveReqVO updateReqVO) {
        // 校验存在
        validateZcfzBaseExists(updateReqVO.getId());
        // 更新
        ZcfzBaseDO updateObj = BeanUtils.toBean(updateReqVO, ZcfzBaseDO.class);
        zcfzBaseMapper.updateById(updateObj);
    }

    @Override
    public void deleteZcfzBase(Long id) {
        // 校验存在
        validateZcfzBaseExists(id);
        // 删除
        zcfzBaseMapper.deleteById(id);
    }

    private void validateZcfzBaseExists(Long id) {
        if (zcfzBaseMapper.selectById(id) == null) {
            throw exception(ZCFZ_BASE_NOT_EXISTS);
        }
    }

    @Override
    public ZcfzBaseDO getZcfzBase(Long id) {
        return zcfzBaseMapper.selectById(id);
    }

    @Override
    public PageResult<ZcfzBaseDO> getZcfzBasePage(ZcfzBasePageReqVO pageReqVO) {
        return zcfzBaseMapper.selectPage(pageReqVO);
    }

}